package com.nlp.visualization.controller.spring.controller;

import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.nlp.visualization.common.CONSTANTS;
import com.nlp.visualization.pojo.common.AjaxReturn;
import com.nlp.visualization.service.backEnd.IModelsService;
import com.nlp.visualization.utils.PropertyUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @author LXM
 * @Title: NLPVisualizationTools
 * @Description:
 * @date 2018/5/19上午11:00
 */
@RequestMapping("/modelsDetail")
@Controller
public class ModelsController {

    @Autowired
    private IModelsService modelsService;

    private static String C_DICPATH = PropertyUtil.getInstance("hanlp").getProperty("root") + "data/dictionary/custom/CustomDictionary.txt";
    private static String C_DICPATH_BIN = PropertyUtil.getInstance("hanlp").getProperty("root") + "data/dictionary/custom/CustomDictionary.txt.bin";


    @RequestMapping("/dic")
    @ResponseBody
    public AjaxReturn getAllDic() {

        return new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "成功", modelsService.findByType("文件"));
    }


    @RequestMapping("/addWords")
    @ResponseBody
    public AjaxReturn addwords(HttpServletRequest request) {

        String word = request.getParameter("word");
        String pos = request.getParameter("pos");
        String p = request.getParameter("p");
        String finalWord = word + " " + pos + " " + p;
        File file = new File(C_DICPATH);
        try {
            CustomDictionary.insert(word, pos + " " + p);


            return new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxReturn(CONSTANTS.ERROR_UNKNOWN, "失败");
        }

    }


}
